$(function () {

    laypage.dir = '/res/lib/laypage/skin/laypage.css ';

    function pageTable(list) {
        var str = '';
        for (var i = 0; i < list.length; i++) {
            str += '<tr data-id="' + list[i].id + '" >' +
                '<td>' + list[i].id + '</td>' +
                '<td>' + list[i].openId + '</td>' +
                '<td>' + list[i].name + '</td>' +
                '<td>' + list[i].phone + '</td>' +
                '<td>' + list[i].address + '</td>' +
                '<td>' + list[i].isDefaultVal + '</td>' +
                '<td>' +
                (function () {
                    if (list[i].isDefault == 0)
                        return '<a class="btn btn-danger btn-xs defaultBtn" data-info="' + list[i].info + '">设为默认</a>';
                    else
                        return '';
                })() +
                '</td>' +
                '</tr>'
        }

        return str
    }


    var memberPage = new PageCreate({
        url:'/address/doPageQuery',
        container:$("#table tbody"),
        pagination:$("#page"),
        success:function (container,data,body) {
            container.html(pageTable(data.list))
            $('.defaultBtn').off('click').on('click',function(){
                var id = $(this).parents('tr').data('id');
                $.restPost('/address/doUpdateDefault',{
                    openId:$.getUrlLocation(),
                    id:id
                }).done(function(mess,data){
                    $.rootSuccessDialog(mess,500);
                    init();
                }).fail(function(mess){
                    $.rootErrorDialog(mess,500);
                })
            })
        }
    });

    var init = function () {

        memberPage.init({
            pageNum: 1,
            pageSize: 10,
            openId:$.getUrlLocation()
        })
    }
    init()
});
